﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using Telerik.Web.UI;


namespace KPIS.GERP.GBMS.APP_UI
{
    public partial class StrategyDataListCombo : System.Web.UI.UserControl
    {
        private RadComboBox ctrChild;
        private IStrategyDataMappingBLL strCtrl = new StrategyDataMappingBLL();
        private IStrategyTypeBLL strTypeCtrl = new StrategyTypeBLL();
        private ComboBoxMode mode;
        private string root;

        public RadComboBox ChildCombo
        {
            get { return ctrChild; }
            set { ctrChild = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadStrategyComboBox(bool isRootNode, string yearstart, ComboBoxMode cbMode)
        {
            string type = "";
            root = (isRootNode)  ? "1" : "0";
            mode = cbMode;
  
            IList<StrategyTypeInfo> strTypeList = new List<StrategyTypeInfo>();

            strTypeList = strTypeCtrl.Search(0, "", "", "", root);

            if (strTypeList.Count > 0)
                type = strTypeList[0].Sequence.ToString();

            LoadStrategyComboBox("", type, "", yearstart);
        }

        public void LoadStrategyComboBox(string seq, string type, string parent, string year_start)
        {
            cbStrategy.DataSource = strCtrl.Search(seq, type, "", parent, year_start, "");
            cbStrategy.DataBind();

            if (mode == ComboBoxMode.SelectAllMode)
            {
                cbStrategy.Items.Insert(0, new RadComboBoxItem("<--- ทั้งหมด --->"));
            }
            else if (mode == ComboBoxMode.SelectTitleMode)
            {
                cbStrategy.Items.Insert(0, new RadComboBoxItem("---- เลือกรายการ ----"));
            }
        }

        protected void cbStrategy_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            int val = 0;

            if (cbStrategy.SelectedIndex > 0)
            {
                val = Convert.ToInt32(cbStrategy.SelectedValue);

                if (ctrChild != null)
                {
                    ctrChild.DataSource = strCtrl.Search("", "", "", val.ToString(), "", "");
                    ctrChild.DataBind();

                    ctrChild.Items.Insert(0, new RadComboBoxItem(cbStrategy.Items[0].Text));
                    ctrChild.Focus();
                }
            }
            else
                if (ctrChild != null)
                    ctrChild.Items.Clear();
        }
    }
}